using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Entites;
namespace DAL
{
   public class DuLichDAL
    {
       
            private SqlConnection cn = null;
        public DuLichDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
       public void Insert (DuLich dl )
        {
            SqlCommand cm = new SqlCommand("DuLich_insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@TenDuLich",dl.tendulich);
           cm.Parameters.AddWithValue("@MaSP",dl.masp);
  

            cm.ExecuteNonQuery();
        }
        public void Update(DuLich dl)
        {
            SqlCommand cm = new SqlCommand("DuLich_update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaDuLich", dl.madulich);
             cm.Parameters.AddWithValue("@TenDuLich",dl.tendulich);
           cm.Parameters.AddWithValue("@MaSP",dl.masp);
          
          
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("DuLich_delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaDuLich", iD);
            cm.ExecuteNonQuery();
        }

        public tbDuLichCollection Get(string where, int pageIndex, int pageSize,
            string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("DuLich_get", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            tbDuLichCollection dlCollection = new tbDuLichCollection();

            while (dr.Read())
            {
                DuLich dulich = new DuLich();
                dulich.madulich = int.Parse(dr.GetValue(0).ToString());
                dulich.tendulich = dr.GetValue(1).ToString();
                dulich.masp = int.Parse(dr.GetValue(2).ToString());
              
                dlCollection.Add(dulich);
             }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return dlCollection;
        }
    }
    }

